﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yahoo.Yui.Compressor;

namespace JSCompressor
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void comButton_Click(object sender, RoutedEventArgs e)
        {
            String codeString = codeBox.Text;
            if (codeString.Length > 0)
            {
                JavaScriptCompressor jsCom = new JavaScriptCompressor();
                jsCom.Encoding = Encoding.UTF8;
                Int32 preLength = codeString.Length;
                try
                {
                    codeString = jsCom.Compress(codeString);
                    codeString += Environment.NewLine;
                    codeBox.Text = codeString;
                    Int32 postLength = codeString.Length;
                    Double ratio = (Double)postLength/preLength*100;
                    String comRatio = ratio.ToString("0.00");
                    resultLabel.Content = String.Format("压缩成功！压缩前：{0}个字符，压缩后：{1}个字符。压缩比：{2}%",preLength,postLength,comRatio
                        );
                }
                catch
                {
                    resultLabel.Content = "压缩失败，你可能是假的JS代码";
                }
            }
            else
            {
                resultLabel.Content = "请输入需要压缩的JS代码";
            }
        }

        private void copyButton_Click(object sender, RoutedEventArgs e)
        {
            if (codeBox.Text.Length > 0) {
                Clipboard.SetDataObject(codeBox.Text);
                resultLabel.Content = "已复制到粘贴板！";
            }
        }

        private void pasteButton_Click(object sender, RoutedEventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                codeBox.Text = (String)iData.GetData(DataFormats.Text);
            }
            else {
                resultLabel.Content = "粘贴板中没有代码";
            }
        }

        private void cssButton_Click(object sender, RoutedEventArgs e)
        {
            String codeString = codeBox.Text;
            if (codeString.Length > 0)
            {
                CssCompressor cssCom = new CssCompressor();
                Int32 preLength = codeString.Length;
                try
                {
                    codeString = cssCom.Compress(codeString);
                    codeString += Environment.NewLine;
                    codeBox.Text = codeString;
                    Int32 postLength = codeString.Length;
                    Double ratio = (Double)postLength / preLength * 100;
                    String comRatio = ratio.ToString("0.00");
                    resultLabel.Content = String.Format("压缩成功！压缩前：{0}个字符，压缩后：{1}个字符。压缩比：{2}%", preLength, postLength, comRatio
                        );
                }
                catch
                {
                    resultLabel.Content = "压缩失败，你可能是假的CSS代码";
                }
            }
            else
            {
                resultLabel.Content = "请输入需要压缩的CSS代码";
            }
        }
    }
}
